package gulimall.pojo.vo;

import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.StringJoiner;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 购物车实体
 *
 * @author HLF
 * @date 2022/8/7 15:45
 */

public class Cart implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 购物项列表，一个购物车可以有多个购物项
     */
    private List<CartItemVO> cartItemVOList;

    /**
     * 购物车的商品总数
     */
    private Long countNum;//get方法计算

    /**
     * 购物车商品类型总数
     */
    private Long countType;//get方法计算

    /**
     * 购物车所有选中商品的总价格(减去优惠价)
     */
    private BigDecimal totalAmount;//get方法计算

    /**
     * 总优惠价(减免价格，默认是0),
     */
    private BigDecimal reduce = new BigDecimal(0);

    /**
     * 获取购物车商品类型总数
     */
    public Long getCountType() {
        if (cartItemVOList != null && cartItemVOList.size() > 0) {
            return (long) cartItemVOList.size();
        }
        return 0L;
    }

    /**
     * 获取购物车商品总数
     */
    public Long getCountNum() {
        AtomicReference<Long> countNum = new AtomicReference<>(0L);
        if (cartItemVOList != null && cartItemVOList.size() > 0) {
            cartItemVOList.forEach(cartItemVO -> {
                countNum.updateAndGet(v -> v + cartItemVO.getCount());
            });
        }
        return countNum.get();
    }

    /**
     * 获取购物车中所有选中商品的总价格
     */
    public BigDecimal getTotalAmount() {
        final BigDecimal[] totalAmount = {new BigDecimal(0)};
        if (cartItemVOList != null && cartItemVOList.size() > 0) {
            cartItemVOList.forEach(cartItemVO -> {
                if (cartItemVO.getCheck()) {//仅计算所有选中的商品的总价格
                    BigDecimal cartItemTotalPrice = cartItemVO.getTotalPrice();
                    totalAmount[0] = totalAmount[0].add(cartItemTotalPrice);
                }
            });
        }
        return totalAmount[0].subtract(reduce);//返回总价格减去总优惠价
    }


    public Cart() {
    }

    public Cart(List<CartItemVO> cartItemVOList, Long countNum, Long countType, BigDecimal totalAmount, BigDecimal reduce) {
        this.cartItemVOList = cartItemVOList;
        this.countNum = countNum;
        this.countType = countType;
        this.totalAmount = totalAmount;
        this.reduce = reduce;
    }

    public List<CartItemVO> getCartItemVOList() {
        return cartItemVOList;
    }

    public void setCartItemVOList(List<CartItemVO> cartItemVOList) {
        this.cartItemVOList = cartItemVOList;
    }

    public void setCountNum(Long countNum) {
        this.countNum = countNum;
    }

    public void setCountType(Long countType) {
        this.countType = countType;
    }

    public void setTotalAmount(BigDecimal totalAmount) {
        this.totalAmount = totalAmount;
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", Cart.class.getSimpleName() + "[", "]")
                .add("cartItemVOList=" + cartItemVOList)
                .add("countNum=" + countNum)
                .add("countType=" + countType)
                .add("totalAmount=" + totalAmount)
                .add("reduce=" + reduce)
                .toString();
    }
}
